package com.graduation.fq.teacher.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.graduation.fq.teacher.R;
import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.logic.InformationLogic;
import com.graduation.fq.teacher.logic.ScoreLogic;
import com.graduation.fq.teacher.module.EventArgs;
import com.graduation.fq.teacher.module.InformationEventArgs;
import com.graduation.fq.teacher.module.StudentInfo;
import com.graduation.fq.teacher.utils.Const;
import com.graduation.fq.teacher.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jifang
 * @version 2016/5/23.
 */
public class StudentListActivity extends Activity {
    private List<StudentInfo> studentInfos;
    private studentAdapter adapter;
    private String courseId;
    private String[] rules = new String[]{"所有成绩", "成绩前10名", "尚未通过的成绩"};
    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        courseId = getIntent().getStringExtra("c_id");
        setContentView(R.layout.activity_student_list);
        ListView studentListView = (ListView) findViewById(R.id.student_list);
        TextView ruleBt = (TextView) findViewById(R.id.student_sort_rule);
        adapter = new studentAdapter();
        studentInfos = new ArrayList<>();
        adapter.setStudentInfoList(studentInfos);
        studentListView.setAdapter(adapter);
        getStudentList(courseId, position);
        ruleBt.setText(rules[0]);
        studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StudentInfo info = studentInfos.get(position);
                showScoreEditDialog(info);
            }
        });
        ruleBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //改变文字
                ((TextView) v).setText(rules[getPosition()]);
                getStudentList(courseId, position);
            }
        });
    }

    private int getPosition() {
        position++;
        if (position > 2) {
            position = 0;
        }
        return position;
    }

    private void showScoreEditDialog(final StudentInfo info) {
        final EditText editText = new EditText(this);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.setMaxEms(10);
        editText.setSingleLine(true);
        new AlertDialog.Builder(this).setTitle("请输入成绩")
                .setView(editText)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        //数据获取
                        String content = editText.getText().toString().trim();
                        if (!TextUtils.isEmpty(content)) {
                            int score = Integer.parseInt(content);
                            info.setScore(score);
                            updateScore(info);
                        } else {
                            ToastUtils.Toast(StudentListActivity.this, "您还未输入成绩");
                        }
                    }
                }).setNegativeButton("取消", null).show();
    }

    /**
     * 更新成绩
     *
     * @param info
     */
    private void updateScore(StudentInfo info) {
        ScoreLogic logic = ScoreLogic.getInstance();
        logic.updateScore(info, courseId, new EventListener() {
            @Override
            public void onEvent(EventArgs args) {
                if (args.getStatus() == Const.HTTP_SUCCESS) {
                    ToastUtils.Toast(StudentListActivity.this, "更新成功");
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void getStudentList(String courseId, int type) {
        InformationLogic logic = InformationLogic.getInstance();
        logic.getStudentList(courseId, type, new EventListener() {
            @Override
            public void onEvent(EventArgs args) {
                if (args.getStatus() == Const.HTTP_SUCCESS) {
                    studentInfos.clear();
                    studentInfos.addAll(((InformationEventArgs) args).getStudentInfos());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private String getUserId() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String id = preferences.getString("user_id", "");
        return id;
    }

    private class studentAdapter extends BaseAdapter {
        private List<StudentInfo> studentInfoList;

        public List<StudentInfo> getStudentInfoList() {
            return studentInfoList;
        }

        public void setStudentInfoList(List<StudentInfo> studentInfoList) {
            this.studentInfoList = studentInfoList;
        }

        @Override
        public int getCount() {
            return studentInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return studentInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            StudentInfo info = studentInfoList.get(position);
            studentViewHolder holder;
            if (convertView == null) {
                holder = new studentViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.layout_student_item, null);
                holder.name = (TextView) convertView.findViewById(R.id.student_item_name);
                holder.score = (TextView) convertView.findViewById(R.id.student_item_score);
                convertView.setTag(holder);
            } else {
                holder = (studentViewHolder) convertView.getTag();
            }

            holder.name.setText(info.getName());
            holder.score.setText(String.valueOf(info.getScore()));
            return convertView;
        }
    }

    private class studentViewHolder {

        TextView name;
        TextView score;

    }
}
